/**
 * Created by Jackie.Chen on 2016/12/4.
 */

var Login = function () {
    /*基础配置*/
    var el = 'body';
    var validateCong = {
        rules: {
            username: {
                required: true,
                remote:{
                    type: "get",
                    url: "/user/verify.do",
                    dataType: "json",
                    cache:false,
                    async:false,
                    data:{
                        username:
                            function() {
                                return $("#username").val();
                            }
                    }
                }
            },
            password: {
                required: true
            },
            code:{
                required: true
            }/*,
            checked:{
                required:true
            }*/
        },
        messages: {
            username: {
                required: "请输入账号",
                remote:"账号不存在"
            },
            password: {
                required: "请输入密码"
            },
            code:{
                required: "请输入验证码"
            }/*,
            checked:{
                required:"请勾选协议"
            }*/
        },
        ignore: ".ignore"
    };

    /*登录*/
    function login() {
            //var callback = [[${callback}]];
            $('#modifyForm').validate(validateCong);
            if ($('#modifyForm').valid()) {
                var dataparam = {
                    "username":  $("#username").val(),
                    "password":  $("#password").val(),
                    "code": $("#code").val()
                };
                if (null != callback) {
                    dataparam["callback"] = callback;
                }
                ajaxCommonFun({
                    type: 'POST',
                    url: '/user/goLogin.do',
                    data: dataparam,
                    success: function (t) {
                        if (t.status == 'ERROR') {
                            layer.msg(t.message);
                            changeCode();
                        } else {
                            layer.msg("登录成功");
                            var isFirstLogin = t.result.firstLogin;
                            if(isFirstLogin==true){
                                location.href = '/user/init.html';
                            }else{
                                if(null != callback) {
                                    window.location.href = callback;
                                }else{
                                    location.href = '/#/product/model-list';
                                }
                            }
                           //
                        }
                    }
                });
            }
    }
    function goRegister() {
        location.href="/user/register.html";
    }
    
    /*切换验证码*/
    function changeCode() {
        var me = this;
        var timestamp = (new Date()).valueOf();
        var url = $('#changeCode')[0].src;
        var num = url.indexOf('/secCodeController/captch.do')+28;
        url = url.substring(0, num); //53
        url = url + "?timestamp=" + timestamp;
        $('#changeCode').attr('src',url)
    }



    /*end分页*/
    /*页面初始化和事件绑定*/
    function bindEvents() {
        $(el).undelegate()
            .delegate('#login','click',login)
            .delegate('#goReg','click',goRegister)
            .delegate('#changeCode','click',changeCode);
    }

    /*页面初始化*/
    function init() {
        bindEvents();
    }
    /*end 页面初始化*/
    init();
};
